#include "../../h/apue.h"
#include <wchar.h>
#include <ctype.h>

#define BSZ 58

int main(void)
{

    FILE * fp;
    char buf[BSZ];

    memset(buf, 'u', BSZ-20);
    buf[4] = 0;
    buf[5] = 'j';
    buf[6] = 'j';
    buf[BSZ - 1] = 0;

    if ((fp = fmemopen(buf, BSZ, "a+")) == NULL)
        err_sys("fmemopen failed");
    printf("initial buffer content: %s\n", buf);

    fprintf(fp, "hello, world");
    fprintf(fp, "aaaa");
    fprintf(fp, "bbb");
    fprintf(fp, "ccc");
    fprintf(fp, "ggggggggggggggggggggggggggggggggggggggggggggg");
    //fprintf(fp, "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
    printf("before flush: %s\n", buf);
    fflush(fp);
    printf("after flush: %s\n", buf);
    fflush(fp);
    printf("after twices flush: %s\n", buf);
    printf("len of string in buf = %ld\n\n", (long)strlen(buf));

//    memset(buf, 'b', BSZ-2);
//    buf[BSZ-2] = 0;
//    buf[BSZ-1] = 'x';
//    fprintf(fp, "Hello, World");
//    fseek(fp, 0, SEEK_SET);
//    printf("after fseek: %s\n", buf);
//    printf("len of string in buf = %ld\n\n", (long)strlen(buf));
//
//    memset(buf, 'c', BSZ-2);
//    buf[BSZ-2] = 0;
//    buf[BSZ-1] = 'x';
//    fprintf(fp, "Zello,  world");
//    fclose(fp);
//    printf("after close: %s\n", buf);
//    printf("len of string in buf = %ld\n", (long)strlen(buf));
//
    return 0;

}
